package com.study.mr.compress;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.CompressionCodecFactory;
import org.apache.hadoop.io.compress.CompressionInputStream;
import org.apache.hadoop.io.compress.CompressionOutputStream;
import org.apache.hadoop.util.ReflectionUtils;
import org.junit.Test;

import java.io.*;

/**
 * 对文件进行压缩和解压缩操作
 */
public class CompressTest {
    //准备原始文件路径
    String srcFile="D:\\hadoopbook\\demodate\\jianai\\jianai.txt.deflate";
    //准备压缩后文件路径
    String destFile="D:\\hadoopbook\\demodate\\jianai\\jianai.txt";
    /**
     * 压缩：通过一个能够具有压缩功能输出流将文件写出
     */
    @Test
    public void testCompress() throws IOException, ClassNotFoundException {
        //声明一个输入流
        FileInputStream in = new FileInputStream(new File(srcFile));
        Configuration conf = new Configuration();
        String classPath="org.apache.hadoop.io.compress.DefaultCodec";
        Class<?> codecClass = Class.forName(classPath);
        //获取一个编解码器（压缩对象）
        CompressionCodec codec = (CompressionCodec) ReflectionUtils.newInstance(codecClass, conf);
        //声明一个输出流，将文件写出
        FileOutputStream out = new FileOutputStream(new File(destFile+codec.getDefaultExtension()));
        //把普通的输出流让CompressionCodec包装一下
        CompressionOutputStream outputStream = codec.createOutputStream(out);
        //读写数据
        IOUtils.copyBytes(in,outputStream,conf);
        //关闭流
        IOUtils.closeStream(in);
        IOUtils.closeStream(outputStream);
    }


    /**
     * 解压缩：通过一个能够具有解压缩功能输出流将文件写出
     */
    @Test
    public void testCodeCompress() throws IOException, ClassNotFoundException {
        //声明一个输入流
        FileInputStream in = new FileInputStream(new File(srcFile));
        Configuration conf = new Configuration();
        //获取一个编解码器(解压缩对象)
        CompressionCodec codec =
                new CompressionCodecFactory(conf).getCodec(new Path(srcFile));
        //把普通的输入流让CompressionCodec包装一下
        CompressionInputStream inputStream = codec.createInputStream(in);

        //声明一个输出流，将文件写出
        FileOutputStream out = new FileOutputStream(new File(destFile));

        //读写数据
        IOUtils.copyBytes(inputStream,out,conf);

        //关闭流
        IOUtils.closeStream(inputStream);
        IOUtils.closeStream(out);

    }

}
